//
//  NYWheel.m
//
//  Created by apple on 15-5-18.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "NYWheel.h"
#import "NYWheelButton.h"
#import "NetWorkTools.h"
#import "ReadLoginData.h"
#define NORMWIDTHA [[UIScreen mainScreen] bounds].size.width / 414
#define NORMHEIGHTA [[UIScreen mainScreen] bounds].size.height / 736
@interface NYWheel()
/**
 *  转盘中间的图片
 */
{
    NSString *strPrise;
    NSInteger time;
    NSInteger num;
    NSInteger user;
}

@property (weak, nonatomic) IBOutlet UIImageView *centerWheel;

//中间的开始按钮
- (IBAction)startBtnClick:(id)sender;

/**
 *  定义属性记录当前选中的按钮
 */

@property (nonatomic, weak) UIButton *selectButton;
@property (nonatomic, strong)UILabel *label;
@property (nonatomic, assign)BOOL isPlayingNow;
@property (nonatomic,strong)CABasicAnimation *basicAnimation;
@property (nonatomic,assign)NSInteger numberIndex;
@end

@implementation NYWheel


- (void)awakeFromNib
{
    [self netWorkGet];
    // 0.让父控件可以交互
    self.centerWheel.userInteractionEnabled = YES;
//    [self setBackgroundColor:[UIColor clearColor]];
    self.label= [[UILabel alloc] init];
    //  [self addSubview: self.label];
    self.label.text  = [NSString stringWithFormat:@"%ld", (long)user];
    self.label.frame = CGRectMake(0, 0, 100*NORMWIDTHA, 50*NORMHEIGHTA);
    self.arr = [ReadLoginData readZhuanPanArr];
    NSArray *arr = [ReadLoginData readZhuanPanArr];
    
    for (int i = 0; i < 8; ++i) {
        self.label = [[UILabel alloc] init];
         self.label.font = [UIFont systemFontOfSize:15];
         self.label.textColor = [UIColor whiteColor];
         self.label.textAlignment = NSTextAlignmentLeft;
        [self.centerWheel addSubview: self.label];
         self.label.frame = CGRectMake(0,0,self.frame.size.width - 25*NORMWIDTHA , 40*NORMHEIGHTA);
         self.label.tag = i;
        // 0.535
         self.label.layer.anchorPoint = CGPointMake(0.5 * WIDTH/414 ,1);
         self.label.layer.position = CGPointMake(self.width * 0.5, self.height * 0.5);
         self.label.transform = CGAffineTransformMakeRotation(i * 45 / 180.0 * M_PI);
        self.label.text = [NSString stringWithFormat:@"%@%@",arr[i][@"Info"],@""];
        if ([arr[i][@"Info"] isEqualToString:@"谢谢参与"]) {
            self.label.text = @"谢谢参与";
        }
   //     self.label.text = [NSString stringWithFormat:@"%d",i];
    }
    
    
}
- (void)netWorkGet{
    
    NSDictionary *params = @{
                             @"MenthodParms":@"",
                             @"Sign":@"CB2E8A4922AF3992A8AE776A4230D9C3E33DD5A0"
                             };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"QUEYMERCAHNTCONFIG" completeBlock:^(id object) {
        NSLog(@"积分转盘数据%@",object);
        NSString *str = [object objectForKey:@"BackData"];
        self.arr = [NetWorkTools arrayWithJsonString:str];
    
//        NSLog(@"objectobjectobject%@",object);
            [ReadLoginData saveZhuanPanToLocal:self.arr];
       
        
    }];
    
    
}
- (void)startRotating
{
    [self addAnim:15 :NSIntegerMax];
}
- (void)stopRotating
{
    [self.centerWheel.layer removeAllAnimations];
    [self addAnim:5 :2];
    [self otherAnim];
    
    
    
}

- (void)addAnim:(float )speed:(float )RepeatCount{
    
    //通过改变view的transform属性值中的z轴坐标来实现旋转
    self.basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    [ self.basicAnimation setDuration:10];
    //开始动画的起始位置
    [ self.basicAnimation setFromValue:[NSNumber numberWithInt:0]];
    //M_PI是180度
    [ self.basicAnimation setToValue:[NSNumber numberWithInt:M_PI * 2]];
    //动画重复次数，无限次
    [ self.basicAnimation setRepeatCount:RepeatCount];
    //播放完毕之后是否逆向回到原来位置
    [ self.basicAnimation setAutoreverses:NO];
    //是否叠加（追加）动画效果
    [ self.basicAnimation setCumulative:YES];
    [self.centerWheel.layer addAnimation: self.basicAnimation forKey:@"basicAnimation"];
    //开始播放
    self.basicAnimation.delegate = self;
    //初始化开始时间
    [self.centerWheel.layer setBeginTime:0];
    //初始化时间偏移量
    [self.centerWheel.layer setTimeOffset:0];
    //设置动画速度
    [self.centerWheel.layer setSpeed:speed];
   
    
}

- (void)otherAnim{
    NSInteger i =  arc4random() % 8;       // 随机数
    NSInteger y = (arc4random() % 1)+5 ; //分角
  NSInteger cash = (arc4random() % 101) ;  //内幕随机数
    NSArray *arr = [ReadLoginData readZhuanPanArr];
    
    NSMutableArray *dataArr = [NSMutableArray array];

    for (NSDictionary *dic in arr) {
        int q =[dic[@"Odds"] floatValue] *100 ;
        [dataArr addObject:[NSString stringWithFormat:@"%d",q]];
    }
  
    
    
    NSInteger a = [dataArr[0] integerValue]+1;
    NSInteger b = [dataArr[1] integerValue]+a;
    NSInteger c = [dataArr[2] integerValue]+b;
    NSInteger d = [dataArr[3] integerValue]+c;
    NSInteger e = [dataArr[4] integerValue]+d;
    NSInteger f = [dataArr[5] integerValue]+e;
    NSInteger g = [dataArr[6] integerValue]+f;
    NSInteger h = [dataArr[7] integerValue]+g-1;
    NSLog(@"随机数。。。 a : %ld,\n b %ld\n,c%ld\n,d%ld\n,e%ld\n,f%ld\n,g%ld\n,h%ld\n",(long)a,b,c,d,e,f,g,h);
    if (cash > 0 && cash<=a) {
       
        self.numberIndex = 7;
    }else if (cash>a &&cash <b ){
      
        self.numberIndex = 0;
    }else if (cash>b &&cash <c){
      
        self.numberIndex = 1;
    }else if (cash>c &&cash <d){
   
        self.numberIndex = 2;
    }else if (cash>d &&cash <e){
    
        self.numberIndex = 3;
    }else if (cash>e &&cash <f){
    
        self.numberIndex = 4;
    }else if (cash>f &&cash <g){
    
        self.numberIndex = 5;
    }else if (cash>g &&cash <h){
        self.numberIndex = 6;
    }
    
    
//    self.numberIndex = i;
    NSLog(@"iiiiiiiiiiiiii  = %ld",self.numberIndex);
    self.centerWheel.transform = CGAffineTransformMakeRotation((M_PI*2/8*(9-self.numberIndex))- (M_PI*2/8/y));
    self.link.paused = YES;
    
    
}

- (void)stopAction{
    CFTimeInterval stopTime = [[[self centerWheel] layer] convertTime:CACurrentMediaTime() fromLayer:nil];
    //停止动画，速度设置为0
    [self.centerWheel.layer setSpeed:0];
    //设置时间偏移量
    [self.centerWheel.layer setTimeOffset:stopTime];
    
}

+ (instancetype)wheel
{
    return [[[NSBundle mainBundle] loadNibNamed:@"NYWheel" owner:nil options:nil] lastObject];
}

- (void)getstr:(NSString *)str result:(JSONBLOCK)block{
    NSInteger i ; ;
    self.numberIndex =self.numberIndex - 1;
    switch (self.numberIndex) {
        case 0:
            i = 2;
            break;
        case 1:
            i = 3;
            break;
        case 2:
            i = 4;
            break;
        case 3:
            i = 5;
            break;
        case 4:
            i = 6;
            break;
        case 5:
            i = 7;
            break;
        case 6:
            i = 0;
            break;
        case 7:
            i = 1;
            break;
        default:
            break;
    }
    
    if (self.numberIndex == -1) {
        i = 1;
    }else if (self.numberIndex == -2){
        
        i = 7;
//        NSLog(@" i = -2 BUG");
    }
//    NSLog(@"iiiiiiiiii%ld", (long)i);
     NSArray *arr = [ReadLoginData readZhuanPanArr];
    NSString *user = [NSString stringWithFormat:@"%@",arr[i][@"Value"]];
    
    NSString *Odds = [NSString stringWithFormat:@"%@",arr[i][@"Odds"]];
 
        block(arr[i]);
    
    
//    NSLog(@"USER %@",user);
    
}




@end
